home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / mmdf / mmdf-IIb.43 / src / submit / mq_wtmail.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-02-28  |  8.3 KB  |  272 lines

  1. #include "util.h"
  2. #include "mmdf.h"
  3.  
  4. /*
  5.  *     MULTI-CHANNEL MEMO DISTRIBUTION FACILITY  (MMDF)
  6.  *     
  7.  *
  8.  *     Copyright (C) 1979,1980,1981  University of Delaware
  9.  *     
  10.  *     Department of Electrical Engineering
  11.  *     University of Delaware
  12.  *     Newark, Delaware  19711
  13.  *
  14.  *     Phone:  (302) 738-1163
  15.  *     
  16.  *     
  17.  *     This program module was developed as part of the University
  18.  *     of Delaware's Multi-Channel Memo Distribution Facility (MMDF).
  19.  *     
  20.  *     Acquisition, use, and distribution of this module and its listings
  21.  *     are subject restricted to the terms of a license agreement.
  22.  *     Documents describing systems using this module must cite its source.
  23.  *
  24.  *     The above statements must be retained with all copies of this
  25.  *     program and may not be removed without the consent of the
  26.  *     University of Delaware.
  27.  *     
  28.  *
  29.  *     version  -1    David H. Crocker    March   1979
  30.  *     version   0    David H. Crocker    April   1980
  31.  *     version  v7    David H. Crocker    May     1981
  32.  *     version   1    David H. Crocker    October 1981
  33.  *
  34.  */
  35. /* *****************  (mq_)  CREATE QUEUED MESSAGES   **************** */
  36.  
  37. /*
  38.  *  Nov 81 Dave Crocker     mq_msinit make message name begin "msg."
  39.  *                          mq_adinit permit no-warning specification
  40.  */
  41.  
  42. #include "ch.h"
  43. #include "adr_queue.h"
  44. #include <sys/file.h>
  45.  
  46. extern char *multcpy();
  47.  
  48. extern LLog *logptr;
  49. extern Chan **ch_tbsrch;
  50. extern char *tquedir,            /* temp directory for address list    */
  51.         *aquedir,            /* actual address list queue          */
  52.         *squepref,           /* string to preface sub-queue name   */
  53.         *mquedir,            /* message text queue                 */
  54.         *supportaddr;     /* where orphaned messages will go    */
  55.  
  56. FILE *mq_mffp;                   /* pointerto message text file buffer */
  57. char    *mq_munique;              /* unique-part of mquedir file name   */
  58.                   /*  public, for use as msg's name     */
  59.  
  60. LOCVAR char *mq_tunique,          /* unique-part of tquedir file name   */
  61.         *mq_aunique,          /* unique-part of aquedir file name   */
  62.         mq_tfname[FILNSIZE],  /* full pathname to tquedir file      */
  63.         mq_afname[FILNSIZE],  /* full pathname to aquedir file      */
  64.         mq_mfname[FILNSIZE];  /* full pathname to mquedir file      */
  65. LOCVAR FILE *mq_tffp;            /* buffer to temporary address file   */
  66. /* */
  67.  
  68. mq_winit ()                       /* initialize to write queued msgs    */
  69. {                                 /* set-up for queue file names        */
  70. #ifdef DEBUG
  71.     ll_log (logptr, LLOGBTR, "mq_init ()");
  72. #endif
  73.  
  74.     /* multcpy used to get pointer to end of base strings */
  75.  
  76.     mq_tunique = multcpy (mq_tfname, tquedir, (char *)0);
  77.     mq_aunique = multcpy (mq_afname, aquedir, (char *)0);
  78.     mq_munique = multcpy (mq_mfname, mquedir, (char *)0);
  79. }
  80. /* */
  81.  
  82. mq_creat ()                      /* initialize for new message         */
  83. {
  84.     static char template[128];
  85.     int fd;
  86.  
  87. #ifdef DEBUG
  88.     ll_log (logptr, LLOGBTR, "mq_creat ()");
  89. #endif
  90. /* make the unique part of the queue entry file name.
  91.  * modelled in behaviour on mktemp() but mktemp() iterates through
  92.  * all the possibilities -- which means too many namei()'s get done
  93.  * so we fake it locally.  allows a maximum of 676 messages per submit...
  94.  */
  95.  
  96.     /* fake like mktemp */
  97.     if (*mq_munique == '\0') {                             
  98.  
  99.     /* get pid */
  100.     sprintf(template,"msg.aa%05.5d",getpid());
  101.  
  102.     /* append template to standard part of name */
  103.     strcpy (mq_munique, template);
  104.     }
  105.  
  106.     while (access(mq_mfname,0) == 0) {
  107.     if (mq_munique[5] >= 'z') {
  108.         if (++mq_munique[4] > 'z')
  109.         break;    /* error caught below */
  110.         mq_munique[5] = 'a';
  111.     }
  112.     else
  113.         mq_munique[5]++;
  114.     }
  115. #ifdef DEBUG
  116.     ll_log (logptr, LLOGFTR, "mname = \"%s\"", mq_mfname);
  117. #endif
  118.  
  119.     if ((fd = creat(mq_mfname, 0444)) < 0 ||
  120.     (mq_mffp = fdopen (fd, "w")) == NULL)
  121.     err_abrt (RP_FCRT, "Can't create text file to be queued.");
  122.                   /* open it now, to reserve the name   */
  123.  
  124.     (void) strcpy (mq_aunique, mq_munique);
  125.     (void) strcpy (mq_tunique, mq_munique);
  126.  
  127. #ifdef DEBUG
  128.     ll_log (logptr, LLOGFTR, "tname = \"%s\"", mq_tfname);
  129. #endif
  130. }
  131.  
  132. /* initialize adddress queue          */
  133. mq_adinit (dowarn, msgflgs, retadr)
  134.     int dowarn;                  /* permit warning message?            */
  135.     int msgflgs;                 /* misc. bits                         */
  136.     char *retadr;                /* return address (NULL or char pointer */
  137. {
  138.     time_t clockdate;            /* variable for clock/date  */
  139.     register Chan **chanptr;
  140.     int fd;
  141.  
  142. #ifdef DEBUG
  143.     ll_log (logptr, LLOGBTR, "mq_adinit ()");
  144. #endif
  145.  
  146.     for (chanptr = ch_tbsrch; *chanptr != 0; chanptr++)
  147.     (*chanptr) -> ch_access &= ~MQ_INQ;
  148.                   /* reset "used" bit, for channel      */
  149.  
  150.     if ((fd = creat(mq_tfname, 0666)) < 0 ||
  151.     (mq_tffp = fdopen (fd, "w")) == NULL)
  152.     err_abrt (RP_FCRT, "Can't create address file to be queued.");
  153.  
  154.     time(&clockdate);             /* store current time, for queue sort */
  155.  
  156. /* Creation, Delivery status, Flags(retcite, for now), return addr */
  157.  
  158.     if (retadr && *retadr)    /* Paranoid */
  159.     fprintf(mq_tffp, "%ld%c%d\n%s\n",
  160.         (long)clockdate, ((dowarn) ? ADR_MAIL : ADR_DONE),
  161.         msgflgs, retadr);
  162.     else
  163.     fprintf(mq_tffp, "%ld%c%d\n%s (Orphanage)\n",
  164.         (long)clockdate, ((dowarn) ? ADR_MAIL : ADR_DONE),
  165.         msgflgs, supportaddr);
  166. }
  167.  
  168. mq_adwrite (achan, ahost, ambox)  /* put an address in the queue */
  169.     register Chan *achan;
  170.     register char *ahost;
  171.     char    *ambox;
  172. {
  173.     char linebuf[LINESIZE];
  174.     static char tmpchr[2] = { ADR_CLR };
  175.     static char typchr[2] = { ADR_MAIL };
  176. #ifdef DEBUG
  177.     ll_log (logptr, LLOGFTR, "%s %8s/%s", achan -> ch_queue, ahost, ambox);
  178. #endif
  179.  
  180. /* this format conforms to the structure specified in adr_queue.h       */
  181.  
  182.     arg2lstr (0, LINESIZE, linebuf, tmpchr, typchr, achan -> ch_queue,
  183.     ahost, ambox, (char *)0);
  184.     fprintf (mq_tffp, "%s\n", linebuf);
  185.     achan -> ch_access |= MQ_INQ; /* message goes into this sub-queue   */
  186. }
  187.  
  188. mq_txinit ()                      /* get ready to write message text    */
  189. {                                 /* text file already opened           */
  190. }
  191. /* */
  192.  
  193. mq_eomsg ()                       /* done submitting message            */
  194. {
  195.     Chan **chanptr;
  196.     char subname[LINESIZE];
  197.  
  198. #ifdef DEBUG
  199.     ll_log (logptr, LLOGBTR, "mq_eomsg ()");
  200. #endif
  201.  
  202.     fflush (mq_mffp);
  203.     if (ferror (mq_mffp))
  204.     err_abrt (RP_FIO, "Problem writing %s", mq_mfname);
  205.  
  206.     if (fclose (mq_mffp) == EOF)
  207.     err_abrt (RP_FIO, "Problem writing %s", mq_mfname);
  208.     
  209.     mq_mffp = (FILE *) NULL;    /* mark it as unusable */
  210.  
  211.     fflush (mq_tffp);
  212.     if (ferror (mq_tffp))
  213.     err_abrt (RP_FIO, "Problem writing %s", mq_tfname);
  214.     if (fclose (mq_tffp) == EOF)
  215.     err_abrt (RP_FIO, "Problem writing %s", mq_tfname);
  216.     mq_tffp = (FILE *) NULL;    /* mark it as unusable */
  217.  
  218.     for (chanptr = ch_tbsrch; *chanptr != 0; chanptr++)
  219.     if ((*chanptr) -> ch_access & MQ_INQ)
  220.     {
  221. #ifdef DEBUG
  222.         ll_log (logptr, LLOGFTR,
  223.             "linking to sub-queue '%s'", (*chanptr) -> ch_queue);
  224. #endif
  225.         sprintf (subname, "%s%s/%s",
  226.                 squepref, (*chanptr) -> ch_queue, mq_munique);
  227.         if (link (mq_tfname, subname) == NOTOK)
  228.         {                         /* add it to the subqueue         */
  229.         sprintf (subname, "%s%s",
  230.                 squepref, (*chanptr) -> ch_queue);
  231.         err_abrt (RP_FCRT, "sub-directory '%s' missing", subname);
  232.         }
  233.     }
  234.  
  235. #ifdef DEBUG
  236.     ll_log (logptr, LLOGFTR,
  237.         "linking to '%s', unlinking '%s'", mq_afname, mq_tfname);
  238. #endif
  239.  
  240.     if (link (mq_tfname, mq_afname) == NOTOK)
  241.     err_abrt (RP_FCRT, "unable to move file to mail queue");
  242.     unlink (mq_tfname);           /* IGNORE ANY ERROR HERE              */
  243. }
  244. /* */
  245.  
  246. mq_clear ()                       /* error termination of queue's files */
  247. {
  248. #ifdef DEBUG
  249.     ll_log (logptr, LLOGBTR, "mq_clear ()");
  250. #endif
  251.  
  252.     if (mq_tffp != (FILE *) EOF && mq_tffp != (FILE *) NULL)
  253.     {                             /* address file                       */
  254.     fclose (mq_tffp);
  255.     mq_tffp = (FILE *) NULL;
  256. #ifdef DEBUG
  257.     ll_log (logptr, LLOGFTR, "unlinking '%s'", mq_tfname);
  258. #endif
  259.     unlink (mq_tfname);
  260.     }
  261.  
  262.     if (mq_mffp != (FILE *) EOF && mq_mffp != (FILE *) NULL)
  263.     {                             /* message text file                  */
  264.     fclose (mq_mffp);
  265.     mq_mffp = (FILE *) NULL;
  266. #ifdef DEBUG
  267.     ll_log (logptr, LLOGFTR, "unlinking '%s'", mq_mfname);
  268. #endif
  269.     unlink (mq_mfname);
  270.     }
  271. }
  272.